package com.yyaccp.easybuy.security.vo;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 模拟从数据库获取用户、角色、权限信息
 * 模拟从数据库获取社交平台配置信息
 */
public class MockData {
    private static Map<String, LoginUser> users = new HashMap<>();
    private static Map<String, SnsPlatform> snsMap = new HashMap<>();
    static {
        users.put("zs", new LoginUser("zs", "$2a$10$pFjGI5DtVrXw0tHXn8X4ceTGSeNbNXVx4pIudjfp9GeO92yNmOhHC", "123",
                Arrays.asList("admin", "user"), Arrays.asList("news:list"), Arrays.asList("GET_/easybuy/test/hello","GET_/easybuy/test/admin")));
        users.put("ls", new LoginUser("ls", "$2a$10$pFjGI5DtVrXw0tHXn8X4ceTGSeNbNXVx4pIudjfp9GeO92yNmOhHC", "123",
                Arrays.asList("user"), Arrays.asList("user:list", "news:list"), Arrays.asList("GET_/easybuy/test/user","GET_/easybuy/test/add")));
        users.put("ww", new LoginUser("ww", "$2a$10$pFjGI5DtVrXw0tHXn8X4ceTGSeNbNXVx4pIudjfp9GeO92yNmOhHC", "123",
                Arrays.asList("guest"), new ArrayList<>(), Arrays.asList("GET_/easybuy/test/list")));
        users.put("13487728688", new LoginUser("13487728688", "$2a$10$pFjGI5DtVrXw0tHXn8X4ceTGSeNbNXVx4pIudjfp9GeO92yNmOhHC", "123",
                Arrays.asList("guest"), new ArrayList<>(), Arrays.asList("GET_/easybuy/test/list")));

        snsMap.put("gitee", new SnsPlatform().setServerUrl("https://gitee.com")
                .setClientId("你的appid")
                .setClientSecret("你的密钥")
                .setCallbackUrl("http://localhost:9999")
                .setAuthorizePath("/oauth/authorize")
                .setTokenPath("/oauth/token")
                .setPlatformName("gitee")
                .setUserInfoUrl("https://gitee.com/api/v5/user"));
        snsMap.put("github", new SnsPlatform().setServerUrl("https://github.com")
                .setClientId("你的appid")
                .setClientSecret("你的密钥")
                .setCallbackUrl("http://localhost:9999")
                .setAuthorizePath("/login/oauth/authorize")
                .setTokenPath("/login/oauth/access_token")
                .setPlatformName("github")
                .setUserInfoUrl("https://api.github.com/user"));
        // https://www.jianshu.com/p/67b372a441d2
        // https://open.dingtalk.com/document/tutorial/scan-qr-code-to-log-on-to-third-party-websites
        snsMap.put("dingtalk", new SnsPlatform().setServerUrl("https://oapi.dingtalk.com")
                .setClientId("你的appid")
                .setClientSecret("你的密钥")
                .setCallbackUrl("http://localhost:9999")
                .setAuthorizePath("/connect/qrconnect")
                .setTokenPath("/connect/oauth2/sns_authorize")
                .setPlatformName("dingtalk")
                .setUserInfoUrl(""));
    }

    public static LoginUser get(String username) {
        return users.get(username);
    }

    public static SnsPlatform getSnsPlatform(String platformName) {
        return snsMap.get(platformName);
    }

    public static void addLoginUser(LoginUser loginUser) {
        users.put(loginUser.getUsername(), loginUser);
    }
    public static void main(String[] args) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        System.out.println("passwordEncoder.encode(\"123\") = " + passwordEncoder.encode("123"));
    }
}
